﻿using AutoMapper;
using B.S.Traceability.Domain;
using B.S.Traceability.Error;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Writer.API.Application.Commends.Health;
using MediatR;

namespace B.S.Traceability.Writer.API.Application.CommendHandlers.Health
{
    public class DelHealthCommendHandlers : IRequestHandler<DelHealthCommends, APIResult<Healths>>
    {

        private readonly IBaseRepository<Healths> healthsRepository;
        private readonly IMapper mapper;

        public DelHealthCommendHandlers(IBaseRepository<Healths> healthsRepository, IMapper mapper)
        {
            this.healthsRepository = healthsRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Healths>> Handle(DelHealthCommends request, CancellationToken cancellationToken)
        {
            APIResult<Healths> result = new APIResult<Healths>();

            result.Code = APIEnum.删除成功;
            result.Msg = "删除成功";
            var farm =  healthsRepository.GetAllAsync().Result.FirstOrDefault(p => p.HealthId == request.HealthId);
            farm.IsDel=!farm.IsDel;
            await healthsRepository.Update(farm);
            return await Task.FromResult(result);
        }
    }
}
